package com.baizhi.hospital.controller;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.baizhi.hospital.util.ValidationCodeUtils;

import org.patchca.service.Captcha;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
class ValidationCodeAction extends HttpServlet {
	@Override
	@RequestMapping("/validateCode.png")
	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// ���ù��߷�����ȡ��֤��
		Captcha c = ValidationCodeUtils.getCaptcha();
		String validationCode = c.getChallenge();
		HttpSession session = request.getSession();
		session.setAttribute("validationCode", validationCode);
		// BuffedImage��������һ��ͼƬ
		BufferedImage bi = c.getImage();
		// ������Ӧ����
		response.setContentType("image/png");
		response.setHeader("cache", "no-cache");
		// ��ͼƬͨ�����������Ӧ�������
		OutputStream out = response.getOutputStream();
		// ��ͼƬbi��png��ʽʹ��out�����
		ImageIO.write(bi, "png", out);
	}
}